package com.lbmb.photodrawaimanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lbmb.photodrawaimanager.entity.dto.PhotoUserUploadEntity;
import com.lbmb.photodrawaimanager.entity.dto.VideoEntity;
import com.lbmb.photodrawaimanager.entity.req.PhotoUserUploadReq;
import com.lbmb.photodrawaimanager.entity.resp.PhotoUserUploadResp;
import com.lbmb.photodrawaimanager.entity.resp.VideoUserResp;
import com.lbmb.photodrawaimanager.mapper.PhotoUserUploadMapper;
import com.lbmb.photodrawaimanager.service.PhotoUserUploadService;
import com.lbmb.photodrawaimanager.utils.OssUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author hanWang
 * @Title:
 * @Package
 * @Description:
 * @date 2023/8/64:18 上午
 */
@Service
public class PhotoUserUploadServiceImpl extends ServiceImpl<PhotoUserUploadMapper, PhotoUserUploadEntity> implements PhotoUserUploadService {

    @Autowired
    PhotoUserUploadMapper photoUserUploadMapper;
    @Autowired
    OssUtils ossUtils;
    @Override
    public Long insert( PhotoUserUploadReq req, MultipartFile file) {
        String fileUrl = ossUtils.uploadOneFile(file);
        PhotoUserUploadEntity entity = new PhotoUserUploadEntity();
        entity.setImgUrl(fileUrl);
        entity.setType(1);
        entity.setVideoId(req.getModuleId());
        entity.setUserId(1L);
        photoUserUploadMapper.insert(entity);
        return entity.getId();
    }

    @Override
    public IPage<PhotoUserUploadResp> selectUserUpLoadByUserId(PhotoUserUploadReq param) {
        Page<PhotoUserUploadEntity> page = new Page(param.getPageNum(), param.getPageNum());
        LambdaQueryWrapper<PhotoUserUploadEntity> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(PhotoUserUploadEntity::getUserId,1L);
            wrapper.eq(PhotoUserUploadEntity::getVideoId,param.getModuleId());
        Page<PhotoUserUploadEntity> result = photoUserUploadMapper.selectPage(page, wrapper);
        IPage<PhotoUserUploadResp> convert = result.convert(a -> {
            PhotoUserUploadResp videoUserResp = new PhotoUserUploadResp();
            BeanUtils.copyProperties(a, videoUserResp);
            return videoUserResp;
        });
        return convert;
    }

    @Override
    public PhotoUserUploadResp selectById(Long uploadId) {
        PhotoUserUploadResp resp = new PhotoUserUploadResp();
        PhotoUserUploadEntity entity = photoUserUploadMapper.selectById(uploadId);
        BeanUtils.copyProperties(entity,resp);
        return resp;
    }
}
